package svgundoredo;

/**
 * This is ungroup command
 */
import java.util.ArrayList;
import svgedit.gui.Frame;
import svgedit.svg.SVGElement;
import svgedit.svg.SVGGroup;

public class UngroupElementCommand extends AbstractCommand
{
    private SVGGroup group;
    private SVGElement elems[];
    private Frame frame;
    
    /**
     * @param group
     *            The selected current group
     * @param frame
     */
    public UngroupElementCommand(SVGGroup group, Frame frame)
    {
        this.group = group;
        this.frame = frame;
    }
    
    @Override
    public void execute()
    {
        // TODO Auto-generated method stub
        ArrayList<SVGElement> selection = new ArrayList<SVGElement>();
        SVGElement[] children = group.getChildren();
        for (SVGElement child : children)
        {
            child.getParent().removeChild(child);
            group.getDocument().getRootGroup().appendChild(child);
            selection.add(child);
        }
        frame.getView().setSelectedElements(
                selection.toArray(new SVGElement[0]));
        elems = frame.getView().getSelectedElements();
        
    }
    
    @Override
    public void undo()
    {
        // TODO Auto-generated method stub
        for (SVGElement elem : elems)
        {
            elem.getParent().removeChild(elem);
            group.appendChild(elem);
        }
        
    }
    
}
